﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-15 16:32:16
// 负责人：liucx
// ===================================================================
using System;
using System.Data;
using System.IO;
using System.Configuration;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using log4net.Layout.Pattern;
using OF.Component.Model;

namespace LogCenter.LogCustom
{
    #region 入库sql和参数
    public class ErrorLogAdoAppender : ErrorLogInfoCustomAdoNetAppender
    {
        public ErrorLogAdoAppender()
        {
            //System.Data.SqlClient
            Type t = typeof(System.Data.SqlClient.SqlConnection);
            string s = t.Assembly.FullName.ToString();
            //数据连接类型
            this.ConnectionType = $"System.Data.SqlClient.SqlConnection, {s}";
            base.UseTransactions = false;
            this.CommandType = CommandType.Text;
            //sql语句
            this.CommandText = @"INSERT INTO ErrorLog			
			(			
			[RequestMethod],
			[RequestUrl],
			[APPVersion],
			[Platform],
			[PlatformVersion],
			[UserID],
			[PostData],
			[UA],
			[Vendor],
			[Model],
			[Token],
			[ClientId],
			[Mobile],
			[Imei],
			[Uuid],
			[CreatedDate],
			[ClientIP],
			[ErrorMessage],
			[ErrorSource],
			[ErrorTrace]
			)
			 VALUES
			(			
			@RequestMethod,
			@RequestUrl,
			@APPVersion,
			@Platform,
			@PlatformVersion,
			@UserID,
			@PostData,
			@UA,
			@Vendor,
			@Model,
			@Token,
			@ClientId,
			@Mobile,
			@Imei,
			@Uuid,
			@CreatedDate,
			@ClientIP,
			@ErrorMessage,
			@ErrorSource,
			@ErrorTrace
			)
			 SET @ErrorLogID=@@IDENTITY
			";
            string parameterName = "";
            parameterName = "ErrorLogID";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int64, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "RequestMethod";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 8, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "RequestUrl";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 2000, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "APPVersion";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "Platform";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int32, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "PlatformVersion";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "UserID";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int64, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "PostData";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = -1, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "UA";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 500, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "Vendor";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "Model";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "Token";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "ClientId";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "Mobile";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 11, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "Imei";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "Uuid";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "CreatedDate";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.DateTime, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "ClientIP";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "ErrorMessage";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = -1, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "ErrorSource";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = -1, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });
            parameterName = "ErrorTrace";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = -1, Layout = new Layout2RawLayoutAdapter(new ErrorLogLayout("%" + parameterName)) });

            base.ActivateOptions();
        }
    }
    #endregion

    #region 数据库连接
    public class ErrorLogInfoCustomAdoNetAppender : AdoNetAppender
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        public new string ConnectionStringName
        {
            set
            {
                this.ConnectionString = ConfigurationManager.ConnectionStrings[value].ConnectionString;
            }
        }
    }
    #endregion

    #region 字段转换
    public class ErrorLogLayout : PatternLayout
    {
        public ErrorLogLayout(string pattern)
            : base(pattern)
        {

        }
        public override void ActivateOptions()
        {

            this.AddConverter("ErrorLogID", typeof(ErrorLogErrorLogIDConverter));
            this.AddConverter("RequestMethod", typeof(ErrorLogRequestMethodConverter));
            this.AddConverter("RequestUrl", typeof(ErrorLogRequestUrlConverter));
            this.AddConverter("APPVersion", typeof(ErrorLogAPPVersionConverter));
            this.AddConverter("Platform", typeof(ErrorLogPlatformConverter));
            this.AddConverter("PlatformVersion", typeof(ErrorLogPlatformVersionConverter));
            this.AddConverter("UserID", typeof(ErrorLogUserIDConverter));
            this.AddConverter("PostData", typeof(ErrorLogPostDataConverter));
            this.AddConverter("UA", typeof(ErrorLogUAConverter));
            this.AddConverter("Vendor", typeof(ErrorLogVendorConverter));
            this.AddConverter("Model", typeof(ErrorLogModelConverter));
            this.AddConverter("Token", typeof(ErrorLogTokenConverter));
            this.AddConverter("ClientId", typeof(ErrorLogClientIdConverter));
            this.AddConverter("Mobile", typeof(ErrorLogMobileConverter));
            this.AddConverter("Imei", typeof(ErrorLogImeiConverter));
            this.AddConverter("Uuid", typeof(ErrorLogUuidConverter));
            this.AddConverter("CreatedDate", typeof(ErrorLogCreatedDateConverter));
            this.AddConverter("ClientIP", typeof(ErrorLogClientIPConverter));
            this.AddConverter("ErrorMessage", typeof(ErrorLogErrorMessageConverter));
            this.AddConverter("ErrorSource", typeof(ErrorLogErrorSourceConverter));
            this.AddConverter("ErrorTrace", typeof(ErrorLogErrorTraceConverter));
            base.ActivateOptions();
        }
    }
    internal sealed class ErrorLogErrorLogIDConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ErrorLogID);
        }
    }
    internal sealed class ErrorLogRequestMethodConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.RequestMethod);
        }
    }
    internal sealed class ErrorLogRequestUrlConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.RequestUrl);
        }
    }
    internal sealed class ErrorLogAPPVersionConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.APPVersion);
        }
    }
    internal sealed class ErrorLogPlatformConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.Platform);
        }
    }
    internal sealed class ErrorLogPlatformVersionConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.PlatformVersion);
        }
    }
    internal sealed class ErrorLogUserIDConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.UserID);
        }
    }
    internal sealed class ErrorLogPostDataConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.PostData);
        }
    }
    internal sealed class ErrorLogUAConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.UA);
        }
    }
    internal sealed class ErrorLogVendorConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.Vendor);
        }
    }
    internal sealed class ErrorLogModelConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.Model);
        }
    }
    internal sealed class ErrorLogTokenConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.Token);
        }
    }
    internal sealed class ErrorLogClientIdConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ClientId);
        }
    }
    internal sealed class ErrorLogMobileConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.Mobile);
        }
    }
    internal sealed class ErrorLogImeiConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.Imei);
        }
    }
    internal sealed class ErrorLogUuidConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.Uuid);
        }
    }
    internal sealed class ErrorLogCreatedDateConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.CreatedDate);
        }
    }
    internal sealed class ErrorLogClientIPConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ClientIP);
        }
    }
    internal sealed class ErrorLogErrorMessageConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ErrorMessage);
        }
    }
    internal sealed class ErrorLogErrorSourceConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ErrorSource);
        }
    }
    internal sealed class ErrorLogErrorTraceConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            ErrorLogInfo log = (ErrorLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ErrorTrace);
        }
    }
    #endregion

}